package com.hw.paltfrom.controller.system;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.ntp.TimeStamp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.hw.paltfrom.mapper.HwBlacklistCarDetailsMapper;
import com.hw.paltfrom.mapper.HwBlacklistCarMapper;
import com.hw.paltfrom.mapper.HwCaseMainMapper;
import com.hw.paltfrom.pack.ResponseResult;
import com.hw.paltfrom.rescode.HWSystemCode;
import com.hw.paltfrom.service.system.SystemServer;
import com.hw.paltfrom.utils.RandomNumberUtil;
import com.hw.paltfrom.utils.VerifyUtil;
import com.hw.paltfrom.vo.BaseUserVo;
import com.hw.paltfrom.vo.SortListVo;

/**
 * 系统通用接口 曹荣坤 2019年11月6日15:06:04
 */

@RestController
@RequestMapping(value = "/system")
public class SyStemController {

	@Autowired
	private HwBlacklistCarMapper blackCarMapper;
	@Autowired
	private HwBlacklistCarDetailsMapper blackCarDetailsMapper;
	@Autowired
	private HwCaseMainMapper HwCaseMainMapper;

	@Autowired
	private SystemServer sortListServer;
	
	
	@Value("${optionPath}")
	private String optionPath;
	
	@Value("${saltFlage}")
	private String saltFlage;
	

	// 小侧边栏
	@RequestMapping(value = "/sortList")
	public Map<String, Object> sortList(SortListVo vo) {
		Boolean flag = VerifyUtil.verifyParams(vo);
		if (!flag) {
			return ResponseResult.fail(HWSystemCode.PARAMS_ERROR);
		}
		return sortListServer.sortList(vo);
	}

	// 主页侧边栏
	@RequestMapping(value = "/indexList")
	public Map<String, Object> indexList(BaseUserVo vo) {
		Boolean flag = VerifyUtil.verifyParams(vo);
		if (!flag) {
			return ResponseResult.fail(HWSystemCode.PARAMS_ERROR);
		}
		return sortListServer.indexList(vo);

	}

	// 主页侧边栏
	@RequestMapping(value = "/mainList")
	public Map<String, Object> mainList(BaseUserVo vo) {
		Boolean flag = VerifyUtil.verifyParams(vo);
		if (!flag) {
			return ResponseResult.fail(HWSystemCode.PARAMS_ERROR);
		}
		return sortListServer.mainList(vo);

	}

	// 文件上传接口
	@RequestMapping(value = "/fileUpload")
	public Map<String, Object> fileUpload(@RequestParam("file") MultipartFile file, @RequestParam("path") String path) {
		try {
			if (file.isEmpty() || StringUtils.isBlank(path)) {
				return ResponseResult.fail(HWSystemCode.PARAMS_ERROR);
			}
			String fileName = file.getOriginalFilename();
//			本地测试
//			 String localPath = "D:\\javaFile\\" + path + "\\" +
//			 System.currentTimeMillis()+"\\"+RandomNumberUtil.getCode()+fileName;
			
//			linux服务器
//			String slat = "/image/" + path + "/" + System.currentTimeMillis() + "/" + RandomNumberUtil.getCode()+ fileName;
//			String localPath = "/usr/tomcat/tomcat8888-8.5.47/webapps/" + slat;

//			 windows服务
//			String slat = "\\image\\" + path + "\\" + System.currentTimeMillis() + "\\" + RandomNumberUtil.getCode()+ fileName;
//			String localPath = "D:\\apache-tomcat-8.5.49-windows-x64\\apache-tomcat-8.5.49\\webapps" + slat;
			//windows server服务
//			String slat = "\\image\\" + path + "\\" + System.currentTimeMillis() + "\\" + RandomNumberUtil.getCode()+ fileName;
//			String localPath = "D:\\jzzc\\apache-tomcat-8.5.47\\webapps" + slat;

			String slat="";
			if(saltFlage.equals("1")){
				slat = "/image/" + path + "/" + System.currentTimeMillis() + "/" + RandomNumberUtil.getCode()+ fileName;
			}else{
				slat = "\\image\\" + path + "\\" + System.currentTimeMillis() + "\\" + RandomNumberUtil.getCode()+ fileName;
			}
			String localPath = optionPath + slat;
			File dest = new File(localPath);

			if (!dest.getParentFile().exists()) {
				dest.getParentFile().mkdirs(); // 不存在则创建父目录
				dest.createNewFile();
			}
			file.transferTo(dest);
			long size = file.getSize();
			String[] split = fileName.split("\\.");
			HashMap<Object, Object> result = new HashMap<>();
			// BigDecimal decimal=new BigDecimal(size);
			// BigDecimal divide = decimal.divide(new BigDecimal(1024));
			// BigDecimal divide2 = divide.divide(new BigDecimal(1024));
			// BigDecimal setScale = divide2.setScale(2, BigDecimal.ROUND_DOWN);
			result.put("size", size);
			result.put("path", slat);
//			result.put("path", localPath);
			result.put("filetype", split[1]);
			result.put("filename", split[0]);
			return ResponseResult.successNotice(HWSystemCode.SUCCESS, result);
		} catch (IOException e) {
			e.printStackTrace();
			return ResponseResult.fail(HWSystemCode.FAIL);
		}
	}

	@RequestMapping(value = "/test")
	public Map<String, Object> test() {
		System.out.println("《---驾驶员黑名单定时任务执行---》");// 异步定时操作
	
		

		return ResponseResult.successNotice(HWSystemCode.SUCCESS, null);
	}
}
